using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Wrath.Xml.Objects;
using Wrath.Xml;

namespace Wrath.RuleForge.Windowing
{
    public class FrameComponent
    {
        private Image topLeft;
        private Image top;
        private Image topRight;
        private Image left;
        private Image middle;
        private Image right;
        private Image bottomLeft;
        private Image bottom;
        private Image bottomRight;
        private Image decal;
        private Rectangle area;
        private FrameDefinition frameDef;

        public FrameComponent(string frameName, Rectangle area)
        {
            this.area = area;

            if (CurrentRuleSet.Frames.ContainsKey(frameName))
            {
                Graphics g;
                Image original = CurrentRuleSet.GetFrameImage(frameName);
                frameDef = new FrameDefinition(CurrentRuleSet.Frames[frameName]);

                // topLeft
                topLeft = new Bitmap(frameDef.TopLeft.Width, frameDef.TopLeft.Height);
                g = Graphics.FromImage(topLeft);
                g.DrawImage(original, 
                            new Rectangle(0, 
                            0, 
                            topLeft.Width, 
                            topLeft.Height),
                            new Rectangle(frameDef.TopLeft.X,
                                          frameDef.TopLeft.Y, 
                                          frameDef.TopLeft.Width, 
                                          frameDef.TopLeft.Height),                            
                            GraphicsUnit.Pixel);

                // topRight
                topRight = new Bitmap(frameDef.TopRight.Width, frameDef.TopRight.Height);
                g = Graphics.FromImage(topRight);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            topRight.Width,
                            topRight.Height),
                            new Rectangle(frameDef.TopRight.X,
                                          frameDef.TopRight.Y,
                                          frameDef.TopRight.Width,
                                          frameDef.TopRight.Height),
                            GraphicsUnit.Pixel);

                // top
                if (frameDef.Top.Width > (area.Width - topLeft.Width - topRight.Width) )
                {
                    top = new Bitmap(area.Width - topLeft.Width - topRight.Width, frameDef.Top.Height);
                }
                else
                {
                    top = new Bitmap(frameDef.Top.Width, frameDef.Top.Height);
                }
                g = Graphics.FromImage(top);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            top.Width,
                            top.Height),
                            new Rectangle(frameDef.Top.X,
                                          frameDef.Top.Y,
                                          frameDef.Top.Width,
                                          frameDef.Top.Height),                            
                            GraphicsUnit.Pixel);                

                // left
                left = new Bitmap(frameDef.Left.Width, frameDef.Left.Height);
                g = Graphics.FromImage(left);
                g.DrawImage(original,
                            new Rectangle(0,
                                            0,
                                            left.Width,
                                            left.Height),
                            new Rectangle(frameDef.Left.X,
                                          frameDef.Left.Y,
                                          frameDef.Left.Width,
                                          frameDef.Left.Height),
                            GraphicsUnit.Pixel);

                // middle
                middle = new Bitmap(frameDef.Middle.Width, frameDef.Middle.Height);
                g = Graphics.FromImage(middle);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            middle.Width,
                            middle.Height),
                            new Rectangle(frameDef.Middle.X,
                                          frameDef.Middle.Y,
                                          frameDef.Middle.Width,
                                          frameDef.Middle.Height),
                            GraphicsUnit.Pixel);

                // right
                right = new Bitmap(frameDef.Right.Width, frameDef.Right.Height);
                g = Graphics.FromImage(right);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            right.Width,
                            right.Height),
                            new Rectangle(frameDef.Right.X,
                                          frameDef.Right.Y,
                                          frameDef.Right.Width,
                                          frameDef.Right.Height),
                            GraphicsUnit.Pixel);

                // bottomLeft
                bottomLeft = new Bitmap(frameDef.BottomLeft.Width, frameDef.BottomLeft.Height);
                g = Graphics.FromImage(bottomLeft);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            bottomLeft.Width,
                            bottomLeft.Height),
                            new Rectangle(frameDef.BottomLeft.X,
                                          frameDef.BottomLeft.Y,
                                          frameDef.BottomLeft.Width,
                                          frameDef.BottomLeft.Height),
                            GraphicsUnit.Pixel);

                // bottom
                bottom = new Bitmap(frameDef.Bottom.Width, frameDef.Bottom.Height);
                g = Graphics.FromImage(bottom);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            bottom.Width,
                            bottom.Height),
                            new Rectangle(frameDef.Bottom.X,
                                          frameDef.Bottom.Y,
                                          frameDef.Bottom.Width,
                                          frameDef.Bottom.Height),
                            GraphicsUnit.Pixel);

                // bottomRight
                bottomRight = new Bitmap(frameDef.BottomRight.Width, frameDef.BottomRight.Height);
                g = Graphics.FromImage(bottomRight);
                g.DrawImage(original,
                            new Rectangle(0,
                            0,
                            bottomRight.Width,
                            bottomRight.Height),
                            new Rectangle(frameDef.BottomRight.X,
                                          frameDef.BottomRight.Y,
                                          frameDef.BottomRight.Width,
                                          frameDef.BottomRight.Height),
                            GraphicsUnit.Pixel);

                // decal
                if (frameDef.Decal != null)
                {
                    decal = new Bitmap(frameDef.Decal.Width, frameDef.Decal.Height);
                    g = Graphics.FromImage(decal);
                    g.DrawImage(original,
                                new Rectangle(0,
                                0,
                                decal.Width,
                                decal.Height),
                                new Rectangle(frameDef.Decal.X,
                                              frameDef.Decal.Y,
                                              frameDef.Decal.Width,
                                              frameDef.Decal.Height),
                                GraphicsUnit.Pixel);
                }

            }
        }

        public void Paint(Graphics graphics)
        {
            // Set Drawing Area.
            area = new Rectangle(0, 0, (int)graphics.VisibleClipBounds.Width, (int)graphics.VisibleClipBounds.Height);

            int topWidth = area.Width - topLeft.Width - topRight.Width;            
            int topRightLeft = area.Width - topRight.Width;
            int leftHeight = area.Height - topLeft.Height - bottomLeft.Height;

            int bottomLeftTop = area.Height - bottomLeft.Height;

            if (topLeft != null)
            {
                graphics.DrawImageUnscaled(topLeft, 0, 0);
            }

            if (top != null)
            {
                graphics.DrawImage(top, new Rectangle(topLeft.Width, 0, topWidth, top.Height), new Rectangle(0,0,top.Width, top.Height), GraphicsUnit.Pixel);
            }

            if (topRight != null)
            {
                graphics.DrawImage(topRight, new Rectangle(topRightLeft, 0, topRight.Width, topRight.Height), new Rectangle(0, 0, topRight.Width, topRight.Height), GraphicsUnit.Pixel);
            }

            if (left != null)
            {
                graphics.DrawImage(left, new Rectangle(0, topLeft.Height, left.Width, leftHeight), new Rectangle(0, 0, left.Width, left.Height), GraphicsUnit.Pixel);
            }

            if (middle != null)
            {
                graphics.DrawImage(middle, new Rectangle(topLeft.Width, topLeft.Height, topWidth, leftHeight), new Rectangle(0, 0, middle.Width, middle.Height), GraphicsUnit.Pixel);
            }

            if (right != null)
            {
                graphics.DrawImage(right, new Rectangle(topRightLeft, topRight.Height, right.Width, leftHeight), new Rectangle(0, 0, right.Width, right.Height), GraphicsUnit.Pixel);
            }

            if (bottomLeft != null)
            {
                graphics.DrawImage(bottomLeft, new Rectangle(0, bottomLeftTop, bottomLeft.Width, bottomLeft.Height), new Rectangle(0, 0, bottomLeft.Width, bottomLeft.Height), GraphicsUnit.Pixel);
            }

            if (bottom != null)
            {
                graphics.DrawImage(bottom, new Rectangle(bottomLeft.Width, bottomLeftTop, topWidth, bottom.Height), new Rectangle(0, 0, bottom.Width, bottom.Height), GraphicsUnit.Pixel);
            }

            if (bottomRight != null)
            {
                graphics.DrawImage(bottomRight, new Rectangle(topRightLeft, bottomLeftTop, bottomRight.Width, bottomRight.Height), new Rectangle(0, 0, bottomRight.Width, bottomRight.Height), GraphicsUnit.Pixel);
            }

            if (decal != null)
            {
                graphics.DrawImage(decal, area);
            }
        }
    }
}
